using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace BodySoundSimulator.CaseData
{
    class AuscultationSiteInfoManipultor
    {
        public static readonly Pen caseInformationDrawingPen = new Pen(Color.Red);
        public static readonly Brush caseInformationDrawingBrush = new SolidBrush(Color.Red);
        static int radius = 5;

        internal static void DrawFrontViewCaseInfo(Graphics g, CaseInformation caseInformation, AuscultationSiteInfo selectedSiteInfo)
        {
            foreach (AuscultationSiteInfo auscultationSiteInfo in caseInformation.FrontViewAuscultationSites)
            {
                if (auscultationSiteInfo == selectedSiteInfo)
                {
                    g.FillEllipse(caseInformationDrawingBrush,
                        auscultationSiteInfo.Location.X - radius,
                        auscultationSiteInfo.Location.Y - radius,
                        radius * 2,
                        radius * 2);
                }
                else
                {
                    g.DrawEllipse(caseInformationDrawingPen,
                        auscultationSiteInfo.Location.X - radius,
                        auscultationSiteInfo.Location.Y - radius,
                        radius * 2,
                        radius * 2);
                }
            }
        }

        internal static void DrawBackViewCaseInfo(Graphics g, CaseInformation caseInformation, AuscultationSiteInfo selectedSiteInfo)
        {
            foreach (AuscultationSiteInfo auscultationSiteInfo in caseInformation.BackViewAuscultationSites)
            {
                if (auscultationSiteInfo == selectedSiteInfo)
                {
                    g.FillEllipse(caseInformationDrawingBrush,
                        auscultationSiteInfo.Location.X - radius,
                        auscultationSiteInfo.Location.Y - radius,
                        radius * 2,
                        radius * 2);
                }
                else
                {
                    g.DrawEllipse(caseInformationDrawingPen,
                        auscultationSiteInfo.Location.X - radius,
                        auscultationSiteInfo.Location.Y - radius,
                        radius * 2,
                        radius * 2);
                }
            }
        }

        internal static bool IsPointOnSite(PointF location, 
            CaseInformation caseInformation, 
            PatientView patientView)
        {
            AuscultationSiteInfo siteInfo = null;
            return IsPointOnSite(location, caseInformation, patientView, ref siteInfo);
        }


        internal static bool IsPointOnSite(PointF location, 
            CaseInformation caseInformation, 
            PatientView patientView, 
            ref AuscultationSiteInfo siteInfo)
        {
            switch (patientView)
            {
                case PatientView.FrontView:
                    foreach (AuscultationSiteInfo site in caseInformation.FrontViewAuscultationSites)
                    {
                        if (DistanceBetweenPoints(site.Location.X,
                            site.Location.Y,
                            location.X,
                            location.Y) <= radius)
                        {
                            siteInfo = site;
                            return true;
                        }
                    }
                    return false;
                case PatientView.BackView:
                    foreach (AuscultationSiteInfo site in caseInformation.BackViewAuscultationSites)
                    {
                        if (DistanceBetweenPoints(site.Location.X,
                            site.Location.Y,
                            location.X,
                            location.Y) <= radius)
                        {
                            siteInfo = site;
                            return true;
                        }
                    }
                    return false;
                default:
                    return false;
            }
        }

        #region Private methods
        private static float DistanceBetweenPoints(float startX, float startY, float endX, float endY)
        {
            return (float)Math.Sqrt((startX - endX) * (startX - endX) + (startY - endY) * (startY - endY));
        }
        #endregion
    }
}
